﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace vJine.Lua.GUI {
    public class ValueTemplateSelector : DataTemplateSelector, IValueConverter {

        public bool IsResult { get; set; }

        #region IValueConverter 成员
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string c = (string)parameter;
            if (c == "BOOL") {
                if (targetType != typeof(bool)) {
                    return false;
                }
                return !(bool)value;
            } else if (c == "RESULT") {
                NodeConfig nConfig = value as NodeConfig;
                if (nConfig == null) {
                    return Visibility.Collapsed;
                }
                if (this.IsResult) {
                    return nConfig.r ?
                        Visibility.Visible : Visibility.Collapsed;
                } else {
                    return !nConfig.r ?
                        Visibility.Visible : Visibility.Collapsed;
                }
            } else if(c == "INDEX") {
                NodeConfig config = value as NodeConfig;
                if (config == null) {
                    return -1;
                } else if (config.list == null || config.list.Count == 0) {
                    return -1;
                } else {
                    for (int i = 0, len = config.list.Count; i < len; i++) {
                        if (config.list[i].Checked) {
                            return i;
                        }
                    }

                    return 0;
                }
            } else {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            string c = (string)parameter;
            if (c == "BOOL") {
                if (targetType != typeof(bool)) {
                    return false;
                }
                return !(bool)value;
            } else {
                if (value == null) {
                    return null;
                }
                if (value.GetType() == typeof(string)) {
                    string vString = value as string;
                    double vDouble = 0;
                    if (double.TryParse(vString, out vDouble)) {
                        return vDouble;
                    }
                }
                return value;
            }
        }
        #endregion

        public DataTemplate BoolTemplate { get; set; }
        public DataTemplate ListSingleTemplate { get; set; }
        public DataTemplate ListMultiTemplate { get; set; }
        public DataTemplate DefaultTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container) {
            if (item != null && item is NodeConfig) {
                NodeConfig config = item as NodeConfig;

                if (config.list != null && config.list.Count > 0) {
                    if (config.multi) {
                        return this.ListMultiTemplate;
                    } else {
                        return this.ListSingleTemplate;
                    }
                } else if (config.value != null && config.value.GetType() == typeof(bool)) {
                    return this.BoolTemplate;
                } else {
                    return this.DefaultTemplate;
                }
            } else {
                return null;
            }
        }
    }
}
